﻿using OpenTK;

namespace Chimp.Core
{
    public class UILayoutFrame : ChimpWidget
    {
        public bool AutoFit;
        public Vector2 BlockSize = new Vector2(64, 32);
        public float BorderSize = 4.0f;
        public bool LeftToRight = true;
        public bool MultiLine = true;

        public UILayoutFrame(float x, float y, float w, float h, ChimpWidget p)
            : base(new Vector2(x, y), new Vector2(w, h), p)
        {
        }

        public override void OnRender()
        {
            base.OnRender();
            if (MultiLine)
            {
                float x, y;
                x = y = 0;
                float big = 0;
                foreach (ChimpWidget w2 in Widgets)
                {
                    w2.Pos = new Vector2(x, y);
                    if (AutoFit)
                    {
                        w2.Size = BlockSize;
                        x += BlockSize.X + BorderSize;
                    }
                    else
                    {
                        x += w2.Size.X + BorderSize;
                    }


                    if ((w2.Pos.X + w2.Size.X) > Size.X - 2)
                    {
                        x = 0;
                        y += big + BorderSize;
                        big = 0;
                        w2.Pos = new Vector2(x, y);
                        x += w2.Size.X + BorderSize;
                    }

                    if (w2.Size.Y > big)
                    {
                        big = w2.Size.Y;
                    }
                }
            }
            else
            {
                if (LeftToRight)

                {
                    float x = 0;
                    foreach (ChimpWidget w2 in Widgets)
                    {
                        w2.Pos = new Vector2(x, 0);
                        x += w2.Size.X + BorderSize;
                    }
                }
                else
                {
                    float y = 0;
                    foreach (ChimpWidget w2 in Widgets)
                    {
                        w2.Pos = new Vector2(0, y);
                        y += w2.Size.Y + BorderSize;
                    }
                }
            }
        }
    }
}